package com.qf.om.servlet;

import com.qf.om.entity.Admin;
import com.qf.om.myutils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@WebServlet(urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String againpassword = req.getParameter("againpassword");
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");
        String[] powers = req.getParameterValues("power");
        String hometown = req.getParameter("hometown");
        System.out.println(username + " " + password + " " + againpassword + " " + name
                + " " + sex + " " + birthday + " " + Arrays.toString(powers) + " " + hometown);
//        效验参数
        if (password==null||"".equals(password)||!password.equals(againpassword)){
            resp.sendRedirect("register.jsp?status=1");
//            执行记得结束
            return;
        }
        if (username==null||"".equals(username)){
            resp.sendRedirect("register.jsp?status=2");
            return;
        }
//        将注册信息转化为实体类
        Admin admin=new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        admin.setName(name);
        admin.setSex(sex);
        admin.setBirthday(birthday);
        admin.setPowers(powers);
        admin.setHometown(hometown);

//       存储到全局变量的集合中 ，模拟数据库
        DataUtils.addAmin(admin);
//        成功则跳转登录页登录
        resp.sendRedirect("login.jsp");
    }
}
